﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using BorsoMediaPlayerEngine.modelos;
using BorsoMediaPlayerEngine.utilitarios;

namespace BorsoMediaPlayer
{
    public partial class FormPrincipal : Form
    {
        #region Variáveis e Constantes
        private Bitmap play = global::BorsoMediaPlayer.Properties.Resources.playButton;
        private Bitmap pause = global::BorsoMediaPlayer.Properties.Resources.pauseButton;
        private Bitmap stop = global::BorsoMediaPlayer.Properties.Resources.stopButton;
        private Bitmap previous = global::BorsoMediaPlayer.Properties.Resources.previousButton;
        private Bitmap next = global::BorsoMediaPlayer.Properties.Resources.nextButton;
        private Bitmap home = global::BorsoMediaPlayer.Properties.Resources.home;

        private AxWMPLib.AxWindowsMediaPlayer mediaPlayer;
        public AxWMPLib.AxWindowsMediaPlayer MediaPlayer
        {
            get {
                if (mediaPlayer == null)
                {
                    mediaPlayer = new AxWMPLib.AxWindowsMediaPlayer();
                }
                return mediaPlayer;
            }
        }
        #endregion

        #region Inicialização
        public FormPrincipal()
        {
            InitializeComponent();
            Size tamanho = new Size(250, 500);
            this.Size = tamanho;
            this.MaximumSize = tamanho;
            this.MinimumSize = tamanho;
            listPlaylist.DisplayMember = "fileName";

            MediaPlayer.InitializeLifetimeService();
            MediaPlayer.NewStream += new EventHandler(teste);
        }
        #endregion

        private void teste(object sender, EventArgs e)
        {

        }

        #region Listener Playlist
        private void btnAdicionaMusica_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "Audio Files|*.mp3;*.wma;*.wav";
            if (DialogResult.OK.Equals(openFileDialog.ShowDialog()))
            {
                adicionaArquivosNoPlaylist(openFileDialog.FileNames);
            }
        }

        private void btnAdicionaPastaDeMusica_Click(object sender, EventArgs e)
        {
            if(DialogResult.OK.Equals(folderDialog.ShowDialog()))
            {
                List<String> arquivos =
                    ValidaArquivo.getArquivosValidos(folderDialog.SelectedPath, "MP3", "WMA", "WAV");
                adicionaArquivosNoPlaylist(arquivos.ToArray());
            }
        }

        private void btnRemoveMusica_Click(object sender, EventArgs e)
        {
            if (listPlaylist.SelectedItems.Count > 0)
            {
                List<Musica> selecionados = UtilListBox<Musica>.clonaSelectedItems(listPlaylist.SelectedItems);
                foreach (Object item in selecionados)
                {
                    listPlaylist.Items.Remove(item);
                }
            }
        }

        private void btnOrdenaMais_Click(object sender, EventArgs e)
        {
        }

        private void btnOrdenaMenos_Click(object sender, EventArgs e)
        {
        }
        #endregion

        #region Listeners Player
        private void btnPlayPause_Click(object sender, EventArgs e)
        {
            MediaPlayer.currentPlaylist.clear();
            IWMPPlaylist listaPlaylist = MediaPlayer.currentPlaylist;
            foreach (Musica musica in listPlaylist.Items)
            {
                listaPlaylist.appendItem(MediaPlayer.newMedia(musica.FilePath));
            }
            MediaPlayer.Ctlcontrols.play();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {

        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {

        }

        private void btnNext_Click(object sender, EventArgs e)
        {

        }

        private void volume_Scroll(object sender, EventArgs e)
        {

        }
        #endregion

        #region Controladores Playlist
        private void adicionaArquivosNoPlaylist(String[] arquivos)
        {
            foreach (String arquivo in arquivos)
            {
                Musica musica = new Musica(arquivo);
                listPlaylist.Items.Add(musica);
            }
        }
        #endregion
    }
}
